<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getScenicSpotById, getScenicSpotTicket } from '@/api/scenicSpot'
import { showToast } from 'vant'

const route = useRoute()
const router = useRouter()
const scenic = ref(null)
const tickets = ref([])
const loading = ref(false)

const loadScenicDetail = async () => {
  try {
    loading.value = true
    const [scenicRes, ticketRes] = await Promise.all([
      getScenicSpotById(route.params.id),
      getScenicSpotTicket(route.params.id)
    ])
    scenic.value = scenicRes.data
    tickets.value = ticketRes.data.ticket
  } catch (error) {
    showToast('加载失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadScenicDetail()
})
</script>

<template>
  <div class="scenic-detail">
    <van-nav-bar 
      :title="scenic?.scenicSpotName" 
      left-arrow 
      @click-left="$router.back()" 
    />
    
    <van-skeleton title :row="10" :loading="loading" v-if="loading" />
    
    <template v-else>
      <div class="header">
        <img :src="scenic?.images" :alt="scenic?.scenicSpotName">
        <div class="overlay">
          <h1>{{ scenic?.scenicSpotName }}</h1>
          <div class="tag">{{ scenic?.tag }}</div>
        </div>
      </div>

      <div class="info-section">
        <div class="info-item">
          <van-icon name="clock-o" />
          <span>开放时间：{{ scenic?.startTime.split(' ')[1].slice(0, 5) }} - {{ scenic?.endTime.split(' ')[1].slice(0, 5) }}</span>
        </div>
        <div class="info-item">
          <van-icon name="phone-o" />
          <span>联系电话：{{ scenic?.phone }}</span>
        </div>
        <div class="info-item">
          <van-icon name="location-o" />
          <span>地址：{{ scenic?.position }}</span>
        </div>
      </div>

      <div class="description-section">
        <h2>景区介绍</h2>
        <p>{{ scenic?.description }}</p>
      </div>

      <div class="tickets-section">
        <h2>门票信息</h2>
        <div class="ticket-list">
          <div v-for="ticket in tickets" :key="ticket.id" class="ticket-item">
            <div class="ticket-info">
              <h3>{{ ticket.ticketName }}</h3>
              <div class="price">
                <span class="sale-price">¥{{ ticket.salePrice }}</span>
                <span class="original-price">¥{{ ticket.price }}</span>
              </div>
            </div>
            <div class="ticket-details">
              <span>剩余: {{ ticket.remainCount }}张</span>
              <span>已售: {{ ticket.saleCount }}张</span>
            </div>
            <van-button 
              type="primary" 
              size="small" 
              block 
              :disabled="ticket.remainCount === 0"
              @click="router.push(`/order/create?ticket=${encodeURIComponent(JSON.stringify(ticket))}`)">
              {{ ticket.remainCount === 0 ? '已售罄' : '立即购买' }}
            </van-button>
          </div>
        </div>
      </div>
    </template>
  </div>
</template>

<style scoped lang="scss">
.scenic-detail {
  min-height: 100vh;
  background: #f8f8f8;

  .header {
    position: relative;
    height: 250px;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .overlay {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      padding: 20px;
      background: linear-gradient(transparent, rgba(0, 0, 0, 0.7));
      color: #fff;

      h1 {
        margin: 0;
        font-size: 24px;
      }

      .tag {
        display: inline-block;
        background: rgba(255, 255, 255, 0.2);
        padding: 4px 8px;
        border-radius: 4px;
        margin-top: 8px;
        font-size: 12px;
      }
    }
  }

  .info-section {
    background: #fff;
    padding: 16px;
    margin-bottom: 12px;

    .info-item {
      display: flex;
      align-items: center;
      margin-bottom: 12px;
      
      .van-icon {
        margin-right: 8px;
        color: #1989fa;
      }
    }
  }

  .description-section, .tickets-section {
    background: #fff;
    padding: 16px;
    margin-bottom: 12px;

    h2 {
      margin: 0 0 16px;
      font-size: 18px;
      color: #333;
    }
  }

  .ticket-list {
    .ticket-item {
      background: #f8f8f8;
      border-radius: 8px;
      padding: 16px;
      margin-bottom: 12px;

      .ticket-info {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 12px;

        h3 {
          margin: 0;
          font-size: 16px;
        }

        .price {
          .sale-price {
            color: #ff6b6b;
            font-size: 20px;
            font-weight: bold;
            margin-right: 8px;
          }

          .original-price {
            color: #999;
            text-decoration: line-through;
            font-size: 14px;
          }
        }
      }

      .ticket-details {
        display: flex;
        justify-content: space-between;
        color: #666;
        font-size: 12px;
        margin-bottom: 12px;
      }

      .van-button {
        &--disabled {
          opacity: 0.5;
          background: #969799 !important;
          border-color: #969799 !important;
        }
      }
    }
  }
}
</style> 